hive 导出数据之一列多行,转为一行多列

需求:提取数据
说明:原数据是一列多行,需要转化为一行多列

待查询表为:temp_05

hive> desc temp_05;
OK
data_dt                 string
zhanghao                string
booking_time_final      string
sign                    string
hname                   string
channel                 string
min_price_weight        string
Time taken: 0.04 seconds, Fetched: 7 row(s)

待查询数据为:

hive>
    > select
    >   booking_time_final,data_dt,min_price_weight
    > from temp_05 where hname like'%兆龙%' and booking_time_final like '%2018-02%' and channel='5'
    > ;

待查询数据如图:
原始数据如图:
需要提取的数据表头如下:

预定日期昨日价格前天价格
2018-02-01
2018-02-02
2018-02-03
2018-02-04

可用提数 SQL

select
  booking_time_final,
  concat_ws('',collect_set(if(data_dt='20180129',min_price_weight,''))) a,
  concat_ws('',collect_set(if(data_dt='20180130',min_price_weight,''))) a2
from temp_05
where hname like'%兆龙%' and booking_time_final like '%2018-02%' and channel='5' 
group by booking_time_final ;

数据如图:
数据如图:


以下为尝试过程

hive>
    > select
    >   if(data_dt='20180129' and booking_time_final='2018-02-01',min_price_weight,'') a,
    >   if(data_dt='20180130' and booking_time_final='2018-02-01',min_price_weight,'') a2
    > from temp_05
    > where hname like'%兆龙%' and channel='5'
    > ;
OK

数据如图:
数据如图:

hive>
    > select
    >     data_dt,booking_time_final,
    >   if(data_dt='20180129' and booking_time_final='2018-02-01',min_price_weight,'') a,
    >   if(data_dt='20180130' and booking_time_final='2018-02-01',min_price_weight,'') a2
    > from temp_05
    > where hname like'%兆龙%' and channel='5'
    > ;

数据如图:
数据如图

hive> select
    >   booking_time_final,
    >   if(data_dt='20180129',min_price_weight,'-') a,
    >   if(data_dt='20180130',min_price_weight,'-') a2
    > from temp_05
    > where hname like'%兆龙%' and booking_time_final like '%2018-02%' and channel='5'
    > ;

数据如图:
数据如图

hive>
    > select
    >   booking_time_final,
    >   collect_set(if(data_dt='20180129',min_price_weight,'-')) a,
    >   collect_set(if(data_dt='20180130',min_price_weight,'-')) a2
    > from temp_05
    > where hname like'%兆龙%' and booking_time_final like '%2018-02%' and channel='5'
    > group by booking_time_final
    > ;

数据如图:
数据如图:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值